package com.zl.api;
/**
 * Launcher APP Opreation
 * @author Engineer-Jsp
 * @date 2014.12.26
 * */
import java.util.List;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
public class RunAppUtil {
//	private static final String TAG = "RunAppUtil";
	private static final String TAG = "AFAdapterService";
	private static PackageManager	mPackageManager;
	private static final String NAVIGATION_KEY = "navi_gation_default";
	public static List<ResolveInfo> APPlistInfo(Context context){
		mPackageManager = context.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);   
        
        List<ResolveInfo> mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
        return mAllApps;
	}
	
	public static void RunAppMethod(Context context,String appname){
		List<ResolveInfo> AllApps = APPlistInfo(context);
        for(int i = 0 ;i< AllApps.size();i++){
        	ResolveInfo res = AllApps.get(i);  
        // setting the application packages name and main Activity  
        String applicationname = res.loadLabel(mPackageManager).toString();
        if(applicationname.equals(appname)){
        String pkg = res.activityInfo.packageName;  
        String cls = res.activityInfo.name;  
        Log.d(TAG,"packages："+pkg+" "+"mainActivity："+cls+" "+"applicationName："+applicationname);
        
        ComponentName componet = new ComponentName(pkg,cls);  
        Intent intent = new Intent(); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(componet);  
        context.startActivity(intent); 
        }
        }
	}
	
	public static void RunProcessMethod(Context context,String pkgname){
		List<ResolveInfo> AllApps = APPlistInfo(context);
        for(int i = 0 ;i< AllApps.size();i++){
        	ResolveInfo res = AllApps.get(i);  
        // setting the application packages name and main Activity  
        String pkg = res.activityInfo.packageName;  
        if(pkg.equals(pkgname)){
        String applicationname = res.loadLabel(mPackageManager).toString();
        String cls = res.activityInfo.name;  
        Log.d(TAG,"packages："+pkg+" "+"mainActivity："+cls+" "+"applicationName："+applicationname);
        
        ComponentName componet = new ComponentName(pkg,cls);  
        Intent intent = new Intent(); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(componet);  
        context.startActivity(intent); 
        }
        }
	}
	
	public static void CloseAppMethod(Context context,String appname){
		List<ResolveInfo> AllApps = APPlistInfo(context);
		for(int i = 0 ;i< AllApps.size();i++){
		 	ResolveInfo res = AllApps.get(i);  
	        // get packagename , do kill application  process opreation  
	        String applicationname = res.loadLabel(mPackageManager).toString();
	        if(applicationname.equals(appname)){
	        String pkg = res.activityInfo.packageName;
//	        KillProcess.kill(pkg); // need root
			ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);       
			manager.killBackgroundProcesses(pkg);
	        
	        Log.d(TAG,"CloseAppMethod Kill Select the Application Method");
			
	        Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.addCategory(Intent.CATEGORY_HOME);
			context.startActivity(intent);
	        }
		}
	}
	public static boolean getRunningProcess(Context context,String pkg){
		boolean isopen = false;
		ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
		// 通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程
		List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager
				.getRunningAppProcesses();

		for (ActivityManager.RunningAppProcessInfo appProcess : appProcessList) {
			String processName = appProcess.processName; // 进程名
			Log.d(TAG, "processName: " + processName );
            if(processName.equals(pkg)){
            	isopen = true;
            }
		}
		return isopen;
	}
	/**
	 * get default launch navi packages name method
	 * 
	 * */
	public static String getDefultPackages(Context context){
		String packages = "";
		// get default packagesName String
		String navi = Settings.System.getString(context.getContentResolver(), NAVIGATION_KEY);
		Log.d(TAG, "get default is packages name :"+navi);
		if(TextUtils.isEmpty(navi)||"com.autonavi.xmgd.navigator".equals(navi)){
			packages = "com.autonavi.xmgd.navigator";
			Log.d(TAG, "because defult navi packages is null,so launch packages:"+packages);
		}
		else{
			packages = navi;
			Log.d(TAG, "because defult navi packages is launch packages:"+navi);
		}
		return packages;
	}
}
